package com.indeep.user.mapper.po;

import com.baomidou.mybatisplus.annotation.*;
import com.indeep.user.dto.InsertPostContentDTO;
import com.indeep.user.dto.IntroductionDTO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.stream.Collectors;

/**
 * @author Laixin
 * @description app_post_content
 * @date 2023-08-29
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("app_post_content")
public class AppPostContentPO implements Serializable {

    private static final long serialVersionUID = 11L;

    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * 标题
     */
    private String title;

    /**
     * 所属社区id
     */
    private Long communityId;

    /**
     * 帖子状态 0——通过 1——文字违规 2——图片违规 3——未审核 4——申诉中 5——已删除
     */
    private Integer status;

    /**
     * 帖子类型 0：普通帖子 1：资源帖子 2：专栏帖子 3：活动帖子
     */
    private Integer type;

    /**
     * 帖子可见时间
     */
    private Date invisibleTime;

    /**
     * 发帖用户id
     */
    private Long userId;

    /**
     * 图片url
     */
    private String imgUrl;

    /**
     * 点赞量
     */
    private Integer likes;

    /**
     * 阅读量
     */
    private Integer NumberOfRead;

    /**
     * 文字内容
     */
    private String text;

    /**
     * 分享链接
     */
    private String links;

    /**
     * 评论量
     */
    private Integer comments;

    /**
     * 转发量
     */
    private Integer forwards;

    /**
     * 视频第一帧
     */
    private String videoCover;

    /**
     * 视频url
     */
    private String videoUrl;

    /**
     * 收藏量
     */
    private Integer collects;

    /**
     * 被举报次数
     */
    private Integer numberOfReports;

    /**
     * 审核员id
     */
    private Long sysUserId;

    /**
     * 审核时间
     */
    private Date auditTime;

    /**
     * 热度
     */
    private Integer hot;

    /**
     * 是否允许评论 0——不允许 1——允许
     */
    private int commentPermission;

    /**
     * @ 谁看
     */
    private String atUserList;

    /**
     * 谁可以看
     */
    private Integer privacyType;

    /**
     * 发帖地点
     */
    private String location;

    /**
     * 发布时ipv4地址
     */
    private Long ipv4;

    /**
     * 特殊帖子的标签
     */
    private String specialTag;

    /**
     * 创建时间-创建时自动填充
     */
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    /**
     * 更新时间-创建与修改时自动填充
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    public AppPostContentPO(InsertPostContentDTO postDto, Long userId, Integer invisible, Integer type) {
        if (postDto.getTitle() != null && !postDto.getTitle().isEmpty()) this.title = postDto.getTitle();
        else {
            if (postDto.getRemark().length() > 11) {
                this.title = postDto.getRemark().substring(0, 11);
            } else {
                this.title = postDto.getRemark();
            }
        }
        this.communityId = postDto.getYdCommunityId();
        this.status = 0;
        this.type = type;
        this.userId = userId;
        this.imgUrl = postDto.getImgUrl();
        this.likes = 0;
        this.NumberOfRead = 0;
        this.text = postDto.getRemark();
        this.links = postDto.getLinks();
        this.comments = 0;
        this.videoCover = postDto.getVideoCover();
        this.videoUrl = postDto.getVideoUrl();
        this.collects = 0;
        this.numberOfReports = 0;
        this.hot = 0;
        this.commentPermission = 1;
        this.location = postDto.getLocation();
        if (postDto.getAtUserList() != null) {
            this.atUserList = postDto.getAtUserList().stream()
                    .map(Object::toString).collect(Collectors.joining("|"));
        }
        this.privacyType = postDto.getPrivacyType();
        if (invisible == 1) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date()); // 设置当前时间

            int days = 6;
            calendar.add(Calendar.MONTH, days);

            invisibleTime = calendar.getTime(); // 获取更改后的时间
        }
        if (invisible == 2) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date()); // 设置当前时间

            int days = 3; // 假设要添加的小时数
            calendar.add(Calendar.DAY_OF_MONTH, days); // 添加小时

            invisibleTime = calendar.getTime(); // 获取更改后的时间
        }
        this.specialTag = postDto.getSpecialTag();
    }

    public AppPostContentPO(Long communityId, Long userId) {
        this.title = "成为Indeeper的第一天。";
        this.communityId = communityId;
        this.status = 0;
        this.type = 0;
        this.userId = userId;
        this.imgUrl = "https://indeep.oss-cn-beijing.aliyuncs.com/%E9%BB%98%E8%AE%A4%E5%9B%BE%E7%89%87/default.png";
        this.likes = 0;
        this.NumberOfRead = 0;
        this.text = "参与社区活动，提供社交反馈，浏览优质内容，共同营造轻松开放的社区环境。";
        this.comments = 0;
        this.collects = 0;
        this.numberOfReports = 0;
        this.hot = 0;
        this.commentPermission = 1;
        this.privacyType = 3;
    }

    public AppPostContentPO(IntroductionDTO dto) {
        this.title = dto.getTitle() + "版规";
        this.communityId = dto.getCommunityId();
        this.status = 0;
        this.type = 0;
        this.userId = 2646L;
        this.text = dto.getContent();
    }
}